package com.lagou.studentsystem.servlet;

import com.lagou.studentsystem.model.Student;
import com.lagou.studentsystem.service.StudentService;
import com.lagou.studentsystem.service.impl.StudentServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UpdateStudentServlet",urlPatterns = "/updateStudent")
public class UpdateStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String oldStudentId = request.getParameter("oldStudentId");
        String m_code = request.getParameter("m_code");
        String m_username = request.getParameter("m_username");
        String m_sex = request.getParameter("m_sex");
        String m_birthday = request.getParameter("m_birthday");
        String m_email = request.getParameter("m_email");
        String stu_remarks = request.getParameter("stu_remarks");
        String stu_class = request.getParameter("stu_class");
        Student student = new Student(m_code,m_username,m_sex,m_birthday,m_email,stu_remarks,stu_class);
        StudentService studentService = new StudentServiceImpl();
        int i = studentService.updateStudentById(oldStudentId, student);
        if (i!=0){
            RequestDispatcher findAllStudent = request.getRequestDispatcher("findStudentAll");
            findAllStudent.forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
